<?php
require_once( '../system/smarty.inc' );
require_once( '../system/tdao/Class_T_MON_CELL.php' );
require_once( '../system/tdao/Class_T_MON_CELL_WEEK.php' );;
require_once( '../system/tdao/Class_T_PATTERN.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/aoki_m_plus.php' );
require_once( '../system/loginauth.inc.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_MON_CELL,$login['staff_auth']);
$isG7  = FALSE;
if   ($login['staff_auth'] == 'G-7') {
	$isG7 = TRUE;
}
$smarty->assign('isG7', $isG7 );
$aja_cd = null;
if ( isset( $_POST['csv'] ) &&  (  $_POST['csv'] == "download" ) ){
	$forms = $_POST;
	
	$_POST["csv"] = "";
	$isfinal = false;
	
	if ( $forms["draft_final"]  == 2 ) {
		$isfinal = true;
	}
	
	$matrix_cd =  $forms["ww"];
	$dao     = new Class_T_MON_CELL_WEEK;
	$dao_mg  = new Class_T_MON_CELL;
	if ($isfinal) {
		$tmpfilename = "final_cellprn".date("Ymd_His");
	} else {
		$tmpfilename = "draft_cellprn".date("Ymd_His");
	}
	
	$matrData = $dao_mg -> getMatrixTerm($matrix_cd);
	$datas    = $dao_mg -> getPrnCellDetails( $matrix_cd, $isfinal, $aja_cd,$matrData["newyear_flag"] );
	//print_r($forms);
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$col_heads = array(
		'title_cd'     => array( 'マトリックスID',     1),
		'contractor'    => array( '印刷会社名',        1),
		'flier_date'    => array( '折込日',            1),
		'title'         => array( 'タイトル',          1),
		'size'          => array( 'サイズ',            1),
		'mano_out'      => array( '管理NO表',          1),
		'mano_in'       => array( '管理NO裏',          1),
		'map_no'      => array( 'マップグループNO',    1),		
		'shop_cd'       => array( '店舗コード',        1),
		'shop_name'     => array( '店舗名',            1),
		'year_mark'     => array( '営業時間',          1),
		'congruent_ins'  => array( '合同折込',          1),
		'psize'         => array( '部数サイズ',        1),
		'pnums'         => array( '部数',              1),
		'pattern_name'  => array( '折込パターン',      1),
		'media_name'    => array( '媒体名',            1),
		'delive_name'   => array( '納品先',            1),
		'delives'       => array( '分納',              1),
		'close_day'    => array( '締切日',            1),
		'close_time'    => array( '締切時間',          1),
		'verupd'        => array( '版変更フラグ',      1),
		'numupd'        => array( '部数変更フラグ',    1),
		'addupd'        => array( '追加フラグ',        1),
		'delupd'        => array( '削除フラグ',        1),
		'c_time'        => array( '新規日時',          3),
		'u_time'        => array( '更新日時',          3),
		'm_user_name'   => array( '更新者名',          1)
	);
		
	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	//データ整形開始
	//$aoki_matrix_contractorname
		$shortime =  strtotime($matrData["flier_date"]) ;
		$flier_date=  date("Y/m/d",$shortime)."(".Class_PWD::$_arr_weeks[date( "w",$shortime)].")";
		$shortime = $shortime - 86400 * 2;
		$close_date=  date("Y/m/d",$shortime);
		
		$k_old     = "";
		$k_old_arr = array();
		$delives_flg = false;
	foreach ($datas as $k => $v) {
		//分納計算開始-------------------------------
		if ($k_old !== "") {
			if (($v['title_cd'] == $datas[$k_old]['title_cd']) && ($v['shop_cd'] == $datas[$k_old]['shop_cd']) && ($v['media_name'] == $datas[$k_old]['media_name'])) {
				$k_old_arr[] = $k;
				if ( ($v['delive_name'] != $datas[$k_old]['delive_name'])) {
					//$datas[$k_old]['delives'] = 1;
					//$datas[$k]['delives'] = 1;
					$delives_flg = true;
				}
			}else {
				if ($delives_flg) {
					foreach ($k_old_arr as $kk => $vv) {
						$datas[$vv]['delives'] = 1;
					}
				}
				$delives_flg = false;
				$k_old_arr   = array();
				$k_old_arr[] = $k;
			}
		}else {
			$k_old_arr[] = $k;
		}
		$k_old = $k;
		//分納計算完了-------------------------------
		$datas[$k]["title"]      = $v['title_cd'] . " ". $v['title'] ;
		$datas[$k]["contractor"] = $aoki_matrix_contractorname[$v["contractor"]];
		$datas[$k]["flier_date"] = $flier_date;
		$datas[$k]["close_date"] = $close_date;
		
		$datas[$k]["c_time"]     =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		$datas[$k]["u_time"]     =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
	}
	if ($delives_flg) {
		foreach ($k_old_arr as $kk => $vv) {
			$datas[$vv]['delives'] = 1;
		}
	}
	
	//データ整形完了
	$dao->output_csv($datas, $colums);
	exit;
}
$year4arr = Class_M_OTHER::get_years_array();
if (empty($forms["year4"])) {
	$forms["year4"] =    current($year4arr);
}
//echo "*****$forms['year4'']******";

if (empty($forms["draft_final"])) {
	$forms["draft_final"] = 1;
}

if( isset($forms) )       $smarty->assign('forms', $forms);
$smarty->assign('year4',  $year4arr);

$draft_final = array(
	'1'  => 'DRAFT',
	'2'  => 'FINAL'
);

$smarty->assign('draft_final', $draft_final);
$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR ,FALSE, TRUE);
$smarty->assign('week', $week );

$smarty->assign('menu',  $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('tmoncellprn_index.html');

?>